﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using MayhemCore;
using MayhemWpf;
using System.IO;
using MayhemWpf.UserControls;
using DefaultModules.Wpf;
using System.Runtime.InteropServices;
using System.Windows.Input;
using System.Windows.Forms;
using MayhemWpf.ModuleTypes;

namespace KeyModule{

    [DataContract]

    [MayhemModule("Write to a file", "Choose which letter is appended to the file")]

    public class KeyPress : ReactionBase, IWpfConfigurable
    {
        [DataMember]
        private string message;
        private string loggerDir = @"C:\MayhemLogs";
        private string loggerFile = "MayhemLogs.txt";
        private string loggerPath;
 
        protected override void OnLoadDefaults()
        {
            loggerFile = "MayhemLogs.txt";
            loggerDir = @"C:\MayhemLogs";
            loggerPath = loggerDir + "\\" + loggerFile;
            
            message = "";
            createFiles();
        }
        public void createFiles()
        {
            loggerFile = "MayhemLogs.txt";
            loggerDir = @"C:\MayhemLogs";
            loggerPath = loggerDir + "\\" + loggerFile;
            if (!System.IO.Directory.Exists(loggerDir))
            {
                System.IO.Directory.CreateDirectory(loggerDir);
            }
            if (!System.IO.File.Exists(loggerPath))
            {
                FileStream fs = File.Create(loggerPath);
                fs.Close();
            }
        }
        public override void Perform()
        {
            //createFiles();
            switch (message)
            {
                case "a":
                    AppendFile("a");
                    break;
                case "b":
                    AppendFile("b");
                    break;
                case "c":
                    AppendFile("c");
                    break;
                case "d":
                    AppendFile("d");
                    break;
                case "e":
                    AppendFile("e");
                    break;
                case "f":
                    AppendFile("f");
                    break;
                case "g":
                    AppendFile("g");
                    break;
                case "h":
                    AppendFile("h");
                    break;
                case "i":
                    AppendFile("i");
                    break;
                case "j":
                    AppendFile("j");
                    break;
                case "k":
                    AppendFile("k");
                    break;
                case "l":
                    AppendFile("l");
                    break;
                case "m":
                    AppendFile("m");
                    break;
                case "n":
                    AppendFile("n");
                    break;
                case "o":
                    AppendFile("o");
                    break;
                case "p":
                    AppendFile("p");
                    break;
                case "q":
                    AppendFile("q");
                    break;
                case "r":
                    AppendFile("r");
                    break;
                case "s":
                    AppendFile("s");
                    break;
                case "t":
                    AppendFile("t");
                    break;
                case "u":
                    AppendFile("u");
                    break;
                case "v":
                    AppendFile("v");
                    break;
                case "w":
                    AppendFile("w");
                    break;
                case "x":
                    AppendFile("x");
                    break;
                case "y":
                    AppendFile("y");
                    break;
                case "z":
                    AppendFile("z");
                    break;
                case "0":
                    AppendFile("0");
                    break;
                case "1":
                    AppendFile("1");
                    break;
                case "2":
                    AppendFile("2");
                    break;
                case "3":
                    AppendFile("3");
                    break;
                case "4":
                    AppendFile("4");
                    break;
                case "5":
                    AppendFile("5");
                    break;
                case "6":
                    AppendFile("6");
                    break;
                case "7":
                    AppendFile("7");
                    break;
                case "8":
                    AppendFile("8");
                    break;
                case "9":
                    AppendFile("9");
                    break;
                case " ":
                    AppendFile(" ");
                    break;
                case "enter":
                    AppendFile("\n");
                    break;
                default:
                    MessageBox.Show("Invalid Input");
                    break;
            }
    
        }
        public void AppendFile(String message)
        {
            loggerFile = "MayhemLogs.txt";
            loggerDir = @"C:\MayhemLogs";
            loggerPath = loggerDir + "\\" + loggerFile;
            Boolean failed = true;
            while (failed)
            {
                try
                {
                    System.IO.StreamWriter w = System.IO.File.AppendText(loggerPath);
                    w.Write(message);
                    w.Flush();
                    w.Close();
                    failed = false;
                }
                catch (Exception e)
                {
                    failed = true;
                }
            }
        }
        public WpfConfiguration ConfigurationControl
        {
            get
            {
                return new KeyPressedControl(message);
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            message = ((KeyPressedControl)configurationControl).Message;
        }

        public string GetConfigString()
        {
            return "Set key to " +  message;
        }
    }
}

